package com.zzk.lanouhn.myapplication1.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.modles.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 新闻界面
 * Created by lanouhn on 16/8/23.
 */
public class NewsFragment extends Fragment implements View.OnClickListener {
    private ImageView icon_title_news, search_title_news;
    private TextView tv_title_news;
    private FragmentManager fm;
    private NewsAllFragment newsAllfragment;
    private NewsEarlyProjectFragment earlyfragment;
    private NewsAfterBFragment afterBfragment;
    private NewsCompanyFragment companyfragment;
    private NewsCapitalFragment capitalfragment;
    private NewsDeepFragment deepfragment;
    private NewsStudyFragment studyfragment;
    private NewsKetvFragment ketvfragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_news, null);
        initView(view);
        initData();
        initEvent();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    private void initView(View view) {
        icon_title_news = (ImageView) view.findViewById(R.id.icon_title_news);
        search_title_news = (ImageView) view.findViewById(R.id.search_title_news);
        tv_title_news = (TextView) view.findViewById(R.id.tv_title_news);
        tv_title_news.setText("新闻");
        fm = getFragmentManager();
        newsAllfragment = new NewsAllFragment();
        earlyfragment = new NewsEarlyProjectFragment();
        afterBfragment = new NewsAfterBFragment();
        companyfragment = new NewsCompanyFragment();
        capitalfragment = new NewsCapitalFragment();
        deepfragment = new NewsDeepFragment();
        studyfragment = new NewsStudyFragment();
        ketvfragment = new NewsKetvFragment();
    }

    private void initData() {
        FragmentTransaction trans = fm.beginTransaction();
        trans.add(R.id.contents_frame, newsAllfragment);
        trans.add(R.id.contents_frame, earlyfragment);
        trans.add(R.id.contents_frame, afterBfragment);
        trans.add(R.id.contents_frame, companyfragment);
        trans.add(R.id.contents_frame, capitalfragment);
        trans.add(R.id.contents_frame, deepfragment);
        trans.add(R.id.contents_frame, studyfragment);
        trans.add(R.id.contents_frame, ketvfragment);
        //trans.hide(newsAllfragment);
        trans.hide(earlyfragment);
        trans.hide(afterBfragment);
        trans.hide(companyfragment);
        trans.hide(capitalfragment);
        trans.hide(deepfragment);
        trans.hide(studyfragment);
        trans.hide(ketvfragment);
        trans.commit();
    }

    /**
     * 处理Eventbus传来的事件：切换content_frame中的内容
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(MessageEvent event) {

        switch (event.fragmentId) {
            case R.id.tv_all_menu_news:
                FragmentTransaction allNews = fm.beginTransaction();
                allNews.show(newsAllfragment);
                allNews.hide(earlyfragment);
                allNews.hide(afterBfragment);
                allNews.hide(companyfragment);
                allNews.hide(capitalfragment);
                allNews.hide(deepfragment);
                allNews.hide(studyfragment);
                allNews.hide(ketvfragment);
                allNews.commitAllowingStateLoss();
                tv_title_news.setText("新闻");
                break;
            case R.id.tv_earlyproject_menu_news:
                FragmentTransaction earlyproject = fm.beginTransaction();
                earlyproject.hide(newsAllfragment);
                earlyproject.show(earlyfragment);
                earlyproject.hide(afterBfragment);
                earlyproject.hide(companyfragment);
                earlyproject.hide(capitalfragment);
                earlyproject.hide(deepfragment);
                earlyproject.hide(studyfragment);
                earlyproject.hide(ketvfragment);
                earlyproject.commitAllowingStateLoss();
                tv_title_news.setText("早期项目");
                break;
            case R.id.tv_afterB_menu_news:
                FragmentTransaction afterb = fm.beginTransaction();
                afterb.hide(newsAllfragment);
                afterb.hide(earlyfragment);
                afterb.show(afterBfragment);
                afterb.hide(companyfragment);
                afterb.hide(capitalfragment);
                afterb.hide(deepfragment);
                afterb.hide(studyfragment);
                afterb.hide(ketvfragment);
                afterb.commitAllowingStateLoss();
                tv_title_news.setText("酷公司");
                break;
            case R.id.tv_company_menu_news:
                FragmentTransaction company = fm.beginTransaction();
                company.hide(newsAllfragment);
                company.hide(earlyfragment);
                company.hide(afterBfragment);
                company.show(companyfragment);
                company.hide(capitalfragment);
                company.hide(deepfragment);
                company.hide(studyfragment);
                company.hide(ketvfragment);
                company.commitAllowingStateLoss();
                tv_title_news.setText("大公司");
                break;
            case R.id.tv_capital_menu_news:
                FragmentTransaction capital = fm.beginTransaction();
                capital.hide(newsAllfragment);
                capital.hide(earlyfragment);
                capital.hide(afterBfragment);
                capital.hide(companyfragment);
                capital.show(capitalfragment);
                capital.hide(deepfragment);
                capital.hide(studyfragment);
                capital.hide(ketvfragment);
                capital.commitAllowingStateLoss();
                tv_title_news.setText("氪友");
                break;
            case R.id.tv_deep_menu_news:
                FragmentTransaction deep = fm.beginTransaction();
                deep.hide(newsAllfragment);
                deep.hide(earlyfragment);
                deep.hide(afterBfragment);
                deep.hide(companyfragment);
                deep.hide(capitalfragment);
                deep.show(deepfragment);
                deep.hide(studyfragment);
                deep.hide(ketvfragment);
                deep.commitAllowingStateLoss();
                tv_title_news.setText("深度");
                break;
            case R.id.tv_study_menu_news:
                FragmentTransaction study = fm.beginTransaction();
                study.hide(newsAllfragment);
                study.hide(earlyfragment);
                study.hide(afterBfragment);
                study.hide(companyfragment);
                study.hide(capitalfragment);
                study.hide(deepfragment);
                study.show(studyfragment);
                study.hide(ketvfragment);
                study.commitAllowingStateLoss();
                tv_title_news.setText("研究");
                break;
//            case R.id.tv_ketv_menu_news:
//                FragmentTransaction ketv = fm.beginTransaction();
//                ketv.hide(newsAllfragment);
//                ketv.hide(earlyfragment);
//                ketv.hide(afterBfragment);
//                ketv.hide(companyfragment);
//                ketv.hide(capitalfragment);
//                ketv.hide(deepfragment);
//                ketv.hide(studyfragment);
//                ketv.show(ketvfragment);
//                ketv.commitAllowingStateLoss();
//                break;
            default:

                break;
        }

    }

    private void initEvent() {
        icon_title_news.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icon_title_news:
                EventBus.getDefault().post(new MessageEvent(R.id.icon_title_news));
                break;

            default:

                break;
        }
    }
}
